<?php
namespace Test;

use Illuminate\Support\Fluent;

class ConvertStudent extends Convert
{
    public function convert($school, $schoolId, $groupinfo, $spaceid, $classId, $class)
    {
        // 获取旧的家长数据
        // 旧的家长数据在学生表里
        // $old = $this->connection->table('yuanbao_v1.t_student')
        //     ->select('t_user.*, t_student.*, t_user_role.roleCode as roleCode')
        //     ->join('yuanbao_v1.t_user', 't_user.username', '=', 't_student.studentCode')
        //     ->join('yuanbao_v1.t_user_role', 't_user_role.username', '=', 't_student.studentCode')
        //     ->where('t_student.classId', '=', $class->id)
        //     ->get();

        $sql = "
SELECT
    t_student.*, t_user.*, t_user_role.roleCode
FROM
    yuanbao_v1.t_student
JOIN (
    SELECT
        email,
        `password`,
        onlineTime,
        loginNum,
        username
    FROM
        yuanbao_v1.t_user
) AS t_user ON t_user.username = t_student.studentCode
JOIN (
    SELECT
        username,
        roleCode
    FROM
        yuanbao_v1.t_user_role
) AS t_user_role ON t_user_role.username = t_student.studentCode
WHERE
    classId = ?
        ";

        $old = $this->connection->select($sql, [$class->id]);


        $check = [
            'ROLE_DIRECTOR' => '校长',
            'ROLE_TEACHER' => '老师',
            'ROLE_PARENT' => '家长',
            'ROLE_DOCTOR' => '医生'
        ];
        foreach ($old as $key => $mix) {

            $mix = new Fluent($mix);
            printf("  Student data convert test, code:%s\n", $mix->studentCode);
            $new = array(
                'user' => $mix->studentCode,
                'name' => $mix->patriarch,
                'space' => $spaceid,
                'password' => $mix->password,
                'email' => empty($mix->email) ? '' : $mix->email,
                'sex' => 0,
                'telphone' => empty($mix->mobile) ? '' : $mix->mobile,
                'avatar' => empty($mix->img) ? '' : $mix->img,
                'created_time' => strtotime($mix->createDate),
                'update_time' => empty($mix->modifyDate) ? 0 : strtotime($mix->modifyDate),
                'user_type' => 0,
                'nickname' => empty($mix->nickName) ? '' : $mix->nickName,
                'login_count' => $mix->loginCount ?: 0,
                'address' => $mix->address ?: '',
                'chat_id' => $mix->studentCode
            );

            $append = [];
            $group = $check[$mix->roleCode];
            foreach ($groupinfo as $value) {
                if ($value['group'] == $group) {
                    $value['select'] = true;
                    $append[] = $value;
                    break;
                }
            }

            // 添加家长账号和学生数据
            $parentid = Tester::addUser($this->connection, $new, $append);
            $student = array(
                'name' => $mix->name,
                'birthday' => strtotime($mix->birthday),
                'parent_id' => $parentid,
                'sex' => $mix->sex,
                'telphone' => $mix->mobile ?: '',
                'allergy' => $mix->allergy ?: '',
                'school_id' => $schoolId,
                'class_id' => $classId,
                'space' => $spaceid,
                'created_time' => strtotime($mix->createDate),
                'update_time' => $mix->modifyDate ? strtotime($mix->modifyDate) : 0,
                'address' => $mix->address ?: '',
                'allergy' => $mix->allergy ?: ''
            );

            $studentId = $this->connection->table('convert.v2_school_student')->insertGetId($student);

            (new ConvertHonor($this->connection))->convert($school, $schoolId, $groupinfo, $spaceid, $classId, $class, $studentId, $mix);
            (new ConvertDayReport($this->connection))->convert($school, $schoolId, $groupinfo, $spaceid, $classId, $class, $studentId, $mix);
        }
    }
}